package botcraft.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.UUID;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.jogamp.nativewindow.awt.AWTGraphicsDevice;

import botcraft.game.Object;
import botcraft.game.ObjectManager;
import botcraft.game.Vector;

public class GUIClient implements Runnable {
	private static final int ACTION_BUTTON_SIZE = 64;
	private static final int ACTION_ROWS = 4;
	private static final int ACTION_COLUMNS = 4;
	
	private ObjectManager objectManager;
	private JFrame frame;
	private GLJPanel gamePanel;
	private JLabel selectedObject;
	final JPanel actionsPanel;
	
	public static void main(final String[] arguments) {
		final GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		
		final GLProfile glProfile = GLProfile.get(new AWTGraphicsDevice(graphicsDevice, 0), GLProfile.GL4);
		for (String profile : GLProfile.GL_PROFILE_LIST_ALL)
			System.out.println(profile);
//		try {
//			final GUIClient guiClient = new GUIClient();
//			SwingUtilities.invokeAndWait(guiClient);
//		} catch (InterruptedException e) {
//		} catch (final InvocationTargetException e) {
//			e.printStackTrace();
//		} catch (final IOException e) {
//			e.printStackTrace();
//		}
	}
	
	public GUIClient() throws IOException {
		objectManager = new ObjectManager();
		frame = new JFrame("GUIClient");
		
		final GLProfile glProfile = GLProfile.getDefault();
		System.out.println(glProfile);
		final GLCapabilities glCapabilities = new GLCapabilities(glProfile);
		
		gamePanel = new GLJPanel(glCapabilities);
		selectedObject = new JLabel();
		actionsPanel = new JPanel();
		objectManager.loadObjectPackage(new URL("file:///Users/thanos/botcraft/units/factory/factory.jar"));
	}
	
	@Override
	public void run() {
		frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
		frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
		final JPanel headerPanel = new JPanel();
		headerPanel.setPreferredSize(new Dimension(frame.getWidth(), ACTION_BUTTON_SIZE));
		headerPanel.setBackground(Color.BLACK);
		final JLabel headerLabel = new JLabel("Header");
		headerLabel.setForeground(Color.WHITE);
		headerPanel.add(headerLabel);
		frame.add(headerPanel);
		final JPanel gamePanel = new JPanel();
		gamePanel.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight() - (ACTION_ROWS + 1) * ACTION_BUTTON_SIZE));
		gamePanel.setBackground(Color.BLACK);
		final JLabel gameLabel = new JLabel("Game");
		gameLabel.setForeground(Color.WHITE);
		gamePanel.add(gameLabel);
		frame.add(gamePanel);
		final JPanel bottomPanel = new JPanel();
		bottomPanel.setPreferredSize(new Dimension(frame.getWidth(), ACTION_BUTTON_SIZE * ACTION_ROWS));
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
		final JPanel mapPanel = new JPanel();
		mapPanel.setPreferredSize(new Dimension(ACTION_BUTTON_SIZE * ACTION_COLUMNS, ACTION_BUTTON_SIZE * ACTION_ROWS));
		mapPanel.setBackground(Color.BLACK);
		final JLabel mapLabel = new JLabel("Map");
		mapLabel.setForeground(Color.WHITE);
		mapPanel.add(mapLabel);
		bottomPanel.add(mapPanel);
		final JPanel infoPanel = new JPanel();
		infoPanel.setPreferredSize(new Dimension(frame.getWidth() - 2 * ACTION_BUTTON_SIZE * ACTION_COLUMNS, ACTION_BUTTON_SIZE * ACTION_ROWS));
		infoPanel.setBackground(Color.BLACK);
		selectedObject.setForeground(Color.WHITE);
		selectedObject.setVerticalTextPosition(JLabel.BOTTOM);
		selectedObject.setHorizontalTextPosition(JLabel.CENTER);
		infoPanel.add(selectedObject);
		bottomPanel.add(infoPanel);
		actionsPanel.setPreferredSize(new Dimension(ACTION_BUTTON_SIZE * ACTION_COLUMNS, ACTION_BUTTON_SIZE * ACTION_ROWS));
		actionsPanel.setLayout(new GridLayout(ACTION_ROWS, ACTION_COLUMNS));
//		for (int i = 0; i < ACTION_ROWS * ACTION_COLUMNS; i++) {
//			final BufferedImage bufferedImage = ImageIO.read(new URL("file:///home/thanos/factory.png"));
//			final JButton button = new JButton(new ImageIcon(bufferedImage.getScaledInstance(ACTION_BUTTON_SIZE, ACTION_BUTTON_SIZE, Image.SCALE_SMOOTH)));
//			button.setToolTipText("Factory");
//			actionsPanel.add(button);
//		}
		bottomPanel.add(actionsPanel);
		frame.add(bottomPanel);
			
		frame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(final KeyEvent keyEvent) {
				if (keyEvent.getKeyCode() == KeyEvent.VK_S) { // select a factory
					final Object object = objectManager.newObject("botcraft.units.factory.Factory", UUID.randomUUID(), new Vector(0.0f, 0.0f, 0.0f), new Vector(0.0f, 0.0f, 0.0f));
					
					selectedObject.setIcon(objectManager.getIcon(object.getClass().getName()));
					selectedObject.setText(objectManager.getDescription(object.getClass().getName()));
					actionsPanel.removeAll();
//					for (JButton action : objectManager.getActions(object.getClass().getName()))
//						actionsPanel.add(action);
				}
			}
		});
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
